$(function () {
    Investment = 1;
    initInvestmentData()

})

//界面数据初始化
function initInvestmentData() {

    let currYear = localStorage.getItem('year');
    let currMon = localStorage.getItem('month');
    let currDept = localStorage.getItem('dept');

    //请求参数
    var obj = {
        budgetYear: currYear,
        budgetMonth: currMon,
        dept: currDept,
        status: 2,
        chartPage: '5010301'
    }

    //API
    ApiGet('getdata', obj, function (e) {

        var json = eval('(' + e + ')')
        var json = JSON.parse(e);
        var chartData = json.items[0].charts;

        setPanelData(chartData[0].data[0], chartData[1].data[0]);
        initTable(chartData[2].data, chartData[3].data)

    });
}

//设置面板 及仪表盘
function setPanelData(data, item) {

    $("#zcze").html((data.key01 / 10000).toFixed(2) + "<span>万元</span>")
    $("#ldzc").html((data.key03 / 10000).toFixed(2) + "<span>万元</span>")
    $("#fldzc").html((data.key04 / 10000).toFixed(2) + "<span>万元</span>")

    //第一个仪表盘
    have('zcChart') ? zcbalance('zcChart', data.key03, data.key01) : ''

    $("#fzze").html((item.key01 / 10000).toFixed(2) + "<span>万元</span>")
    $("#ldfz").html((item.key03 / 10000).toFixed(2) + "<span>万元</span>")
    $("#fldfz").html((item.key04 / 10000).toFixed(2) + "<span>万元</span>")

    var srting = data.key01 == 0 ? 0 : ((item.key01 / data.key01).toFixed(2)) * 100

    $("#zcfzl").html(srting + "<span>%</span>")

    //第二个仪表盘
    have('fzChart') ? fzbalance('fzChart', item.key03, item.key01) : ''

}

//插入表格
function initTable(df, dr) {

    var strHtml = '';
    var cssFlag = '';
    var arrow = 'cash-down';
    for (var i = 0; i < df.length; i++) {
        cssFlag = 'normal'
        if (df[i].key06 == '1') {
            cssFlag = 'summary'
        }
        if (df[i].key07 == '1') {
            cssFlag = 'summaryTotal'
        }

        if (df[i].key05 > 0) {
            arrow = 'cash-up'
        } else if (df[i].key05 == 0) {
            arrow = 'hide'
        } else {
            arrow = 'cash-down'
        }
        strHtml += ` <tr class="${cssFlag}">
                        <td>${(df[i].key01).replace(' ', '&nbsp;')}</td>
                        <td>${df[i].key03}</td>
                        <td>${df[i].key04}</td>
                        <td class="${arrow}"><span class=""></span> ${df[i].key05 == '0' ? '' : df[i].key05}</td>
                    </tr>`;

    }

    $("#zctable tbody").append(strHtml);

    var strHtml = '';
    for (var i = 0; i < dr.length; i++) {
        cssFlag = 'normal'
        if (dr[i].key06 == '1') {
            cssFlag = 'summary'
        }
        if (dr[i].key07 == '1') {
            cssFlag = 'summaryTotal'
        }

        if (dr[i].key05 > 0) {
            arrow = 'cash-up'
        } else if (dr[i].key05 == 0) {
            arrow = 'hide'
        } else {
            arrow = 'cash-down'
        }

        strHtml += ` <tr class="${cssFlag}">
                        <td>${(dr[i].key01).replace(' ', '&nbsp;')}</td>
                        <td>${dr[i].key03}</td>
                        <td>${dr[i].key04}</td>
                        <td class="${arrow}"><span class=""></span> ${dr[i].key05 == '0' ? '' : dr[i].key05}</td>
                    </tr>`;

    }

    $("#fztable tbody").append(strHtml);
}

